package com.chushouya.product.service.admin.impl;


import com.general.framework.core.lang.*;
import com.chushouya.common.support.Contexts;
import com.chushouya.common.util.PinYinUtil;
import com.general.framework.core.enums.StatusEnum;
import com.general.framework.core.schema.PageResult;
import com.chushouya.product.dao.entity.BrandEntity;
import com.chushouya.product.dao.repository.BrandRepository;
import com.chushouya.product.dto.admin.brand.BrandDetailDTO;
import com.chushouya.product.dto.admin.brand.BrandListDTO;
import com.chushouya.product.dto.admin.brand.BrandQuery;
import com.chushouya.product.service.admin.BrandService;
import com.chushouya.product.service.admin.CategoryBrandService;
import com.chushouya.product.service.admin.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
@Slf4j
public class BrandServiceImpl implements BrandService {

    @Resource
    private BrandRepository brandRepository;

    @Resource
    private ProductService productService;

    @Resource
    private CategoryBrandService categoryBrandService;

    @Override
    public PageResult<BrandListDTO> selectBrandPageResult(BrandQuery query) {
        query.setDelFlag(Numbers.INTEGER_ZERO);
        PageResult<BrandEntity> pageResult = brandRepository.selectPageResult(query);
        return PageResult.of(pageResult, BrandListDTO.class);
    }

    @Override
    public BrandDetailDTO getBrandDetail(Long brandId) {
        Check.notNull(brandId, "brandId can not be null");
        return Beans.copy(brandRepository.selectByPrimaryKey(brandId), BrandDetailDTO.class);
    }

    @Override
    public Long addBrand(BrandDetailDTO brand) {
        Check.notEmpty(brand.getBrandName(), "品牌名称不能为空");
        BrandQuery query = new BrandQuery();
        query.setBrandName(brand.getBrandName());
        BrandEntity brandEntity = brandRepository.selectByBrandName(query);
        Validate.isNotNull(brandEntity, "品牌名称已存在");

        brandEntity = Beans.copy(brand, BrandEntity.class);
        brandEntity.setInitial(PinYinUtil.getFirstIndexSpellUpperCase(brand.getBrandName()));
        brandEntity.setStatus(StatusEnum.normal.value());
        brandEntity.setCreateTime(Dates.getTimeNow());
        brandEntity.setUpdateTime(Dates.getTimeNow());
        brandEntity.setHandleUser(Contexts.getUserContext().getUserName());
        brandRepository.insertSelective(brandEntity);
        return brandEntity.getBrandId();
    }

    @Override
    public Long editBrand(BrandDetailDTO brand) {
        Check.notNull(brand.getBrandId(), "brandId can not be null");
        Check.notEmpty(brand.getBrandName(), "品牌名称不能为空");
        final BrandEntity brandEntity = brandRepository.selectByPrimaryKey(brand.getBrandId());
        Check.notNull(brandEntity, "品牌不存在");
        BrandEntity updateEntity = Beans.copy(brand, BrandEntity.class);
        updateEntity.setInitial(PinYinUtil.getFirstIndexSpellUpperCase(brand.getBrandName()));
        updateEntity.setUpdateTime(Dates.getTimeNow());
        updateEntity.setHandleUser(Contexts.getUserContext().getUserName());
        brandRepository.updateByPrimaryKeySelective(updateEntity);
        if (!Objects.equals(brand.getBrandName(), brandEntity.getBrandName())) {
           productService.syncBrandName(brand.getBrandId(), brand.getBrandName());
        }
        return updateEntity.getBrandId();
    }

    @Override
    public void removeBrand(Long brandId) {
        Check.notNull(brandId, "brandId can not be null");
        Check.notTrue(productService.hasProductBindBrand(brandId), "有商品关联当前品牌，不能删除");
        Check.notTrue(categoryBrandService.hasCategoryBindBrand(brandId), "有分类关联当前品牌，不能删除");
        BrandEntity brandEntity = brandRepository.selectByPrimaryKey(brandId);
        Check.notNull(brandEntity, "品牌不存在");
        BrandEntity updateEntity = new BrandEntity();
        updateEntity.setBrandId(brandId);
        updateEntity.setDelFlag(Numbers.INTEGER_ONE);
        updateEntity.setUpdateTime(Dates.getTimeNow());
        brandRepository.updateByPrimaryKeySelective(updateEntity);
    }

    @Override
    public Long editBrandStatus(Long brandId, Integer status) {
        Check.notNull(brandId, "brandId can not be null");
        BrandEntity updateEntity = new BrandEntity();
        updateEntity.setBrandId(brandId);
        updateEntity.setStatus(status);
        updateEntity.setUpdateTime(Dates.getTimeNow());
        updateEntity.setHandleUser(Contexts.getUserContext().getUserName());
        brandRepository.updateByPrimaryKeySelective(updateEntity);
        return brandId;
    }

    @Override
    public String getBrandName(Long brandId) {
        return brandRepository.selectByPrimaryKey(brandId).getBrandName();
    }

}
